package com.cskaoyan.controller.promotion;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.CommonData;
import com.cskaoyan.bean.bo.promotionbo.AdminBo;
import com.cskaoyan.bean.po.promotionpo.MarketAd;
import com.cskaoyan.exception.MarketValidationException;
import com.cskaoyan.service.promotion.AdminAdService;
import com.cskaoyan.util.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zhang tao
 * @Description:
 * @Date: 2022/7/15 16:56
 */
@RestController
@RequestMapping("admin/ad")
public class AdminAdController {

    @Autowired
    AdminAdService adminAdService;


    @GetMapping("list")
    public BaseRespVo adminAdList(AdminBo adminBo) {
        CommonData<MarketAd> adList = null;
        try {
            adList = adminAdService.getAds(adminBo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(adList);
    }
    @PostMapping("create")
    public BaseRespVo adminAdCreate(@Validated @RequestBody MarketAd marketAd,BindingResult result) throws MarketValidationException {
        ValidationUtils.valid(result);
        MarketAd marketAdResp = null;
        try {
            marketAdResp= adminAdService.insertAds(marketAd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(marketAdResp);
    }
    @PostMapping("update")
    public BaseRespVo adminAdUpdate(@Validated @RequestBody MarketAd marketAd,BindingResult result) throws MarketValidationException {
        ValidationUtils.valid(result);
        MarketAd marketAdResp = null;
        try {
            marketAdResp= adminAdService.updateAds(marketAd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(marketAdResp);
    }
    @PostMapping("delete")
    public BaseRespVo adminAdDelete(@RequestBody MarketAd marketAd) {

        try {
          adminAdService.deleteAds(marketAd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(null);
    }
    @GetMapping("read")
    public BaseRespVo readAdminAd( Integer id) {
        MarketAd marketAdResp = null;
        try {
            marketAdResp = adminAdService.readAdminAd(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(marketAdResp);
    }

}
